import 'package:flutter/material.dart';

/*
父组件管理状态
 */
class AllManageDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _AllManageDemoState()
    ;
  }

}

class _AllManageDemoState extends State<AllManageDemo> {

  bool _active = false;

  void _handleTapChange(bool newValue) {
    setState(() {
      _active = newValue;
    });
  }

  @override
  Widget build(BuildContext context) {
    var mineManage = new GestureDetector(
      child: new BoxC(
        onChanged: _handleTapChange,
        active: _active,
      ),
    );

    return new Scaffold(
      appBar: new AppBar(title: new Text('自己管理状态'),),
      body: mineManage,
    );
  }
}

class BoxC extends StatefulWidget {

  BoxC({Key key, this.active: false, @required this.onChanged})
      : super(key: key);

  bool active;

  final ValueChanged<bool> onChanged;

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _BoxCState();
  }
}


class _BoxCState extends State<BoxC> {

  bool _highlight = false;

  void _handleTapDown(TapDownDetails details) {
    setState(() {
      _highlight = true;
    });
  }

  void _handleTapUp(TapUpDetails details) {
    setState(() {
      _highlight = false;
    });
  }

  void _handleTapCancel() {
    setState(() {
      _highlight = false;
    });
  }

  void _handleTap() {
    widget.onChanged(!widget.active);
  }

  @override
  Widget build(BuildContext context) {
    return new GestureDetector(
      onTapDown: _handleTapDown,
      // Handle the tap events in the order that
      onTapUp: _handleTapUp,
      // they occur: down, up, tap, cancel
      onTap: _handleTap,
      onTapCancel: _handleTapCancel,
      child: new Container(
        child: new Center(
          child: new Text(widget.active ? 'Active' : 'Inactive',
              style: new TextStyle(fontSize: 32.0, color: Colors.white)),
        ),
        width: 200.0,
        height: 200.0,
        decoration: new BoxDecoration(
          color:
          widget.active ? Colors.lightGreen[700] : Colors.grey[600],
          border: _highlight
              ? new Border.all(
            color: Colors.teal[700],
            width: 10.0,
          )
              : null,
        ),
      ),
    );
  }
}